package com.cl.edu.web.admin;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.cl.common.CrudActionSupport;
import com.cl.core.orm.Page;
import com.cl.core.web.struts2.Struts2Utils;
import com.cl.edu.entity.Message;
import com.cl.edu.entity.Student;
import com.cl.edu.entity.Teacher;
import com.cl.edu.service.admin.StudentsManager;

/**
 * 学员信息管理
 * @author Administrator
 *
 */
public class StudentAction extends CrudActionSupport<Student>{

	@Autowired
	private StudentsManager studentManager;
	
	private Student entity;	
	private Long id;
	
	private Page<Student> page = new Page<Student>();
	
	private String filter_id;
	private String filter_name;
	
	/**
	 * 查询学员列表
	 */
	@Override
	public String list() throws Exception {
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = studentManager.getStudents(page,0);
		return SUCCESS;
	}
	
	/**
	 * 查询所有未通过审核的学员列表
	 * @return
	 * @throws Exception
	 */
	public String uncheck() throws Exception {
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = studentManager.getStudents(page,1);
		return "uncheck";
	}
	
	/**
	 * 通过教员审核
	 * @return
	 * @throws Exception
	 */
	public String check() throws Exception {
		Student student = studentManager.getStudent(id);
		student.setState(0);
		studentManager.updateStudent(student);//审核通过
		
		addActionMessage("操作成功!");
		return uncheck();
	}
	
	/**
	 * 修改学员被介绍次数
	 * @return
	 * @throws Exception
	 */
	public String changenum() throws Exception {
		Student student = studentManager.getStudent(id);
		String introduceNum = Struts2Utils.getParameter("introduceNum");
		student.setIntroduceNum(introduceNum);
		studentManager.updateStudent(student); //被介绍次数修改
		
		addActionMessage("次数修改成功!");
		return list();
	}
	
	/**
	 * 查看一个学员全部注册的详细信息
	 * @return
	 * @throws Exception
	 */
	public String detail() throws Exception{
		entity = studentManager.getStudent(id);
		return "detail";
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 删除学员信息
	 */
	@Override
	public String delete() throws Exception {
		int state = studentManager.getStudent(id).getState();
		studentManager.delBookingstudents(id);//删除有关联的预约信息
		studentManager.delBookingteachers(id);
		studentManager.delStudent(id);
		
		addActionMessage("删除学员信息成功!");
		if(state == 0){
			return list();
		}else {
			return uncheck();
		}
	}

	@Override
	protected void prepareModel() throws Exception {
		if(id == null){
			entity = new Student();
		}else {
			entity = studentManager.getStudent(id);
		}
	}
	
	public Student getModel() {
		return entity;
	}


	public Student getEntity() {
		return entity;
	}

	public void setEntity(Student entity) {
		this.entity = entity;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Page<Student> getPage() {
		return page;
	}

	public void setPage(Page<Student> page) {
		this.page = page;
	}

	public String getFilter_name() {
		return filter_name;
	}

	public void setFilter_name(String filter_name) {
		this.filter_name = filter_name;
	}

	public String getFilter_id() {
		return filter_id;
	}

	public void setFilter_id(String filterId) {
		filter_id = filterId;
	}


}
